home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
CTEX__
/
GFREAD.C
next >
Wrap
Text File
|
1987-05-25
|
15KB
|
491 lines
/* GFread in C---Tomas Rokicki */
#include <stdio.h>
#define linelength 79
#define terminallinelength 150
#define memmax 4000
#define incr(a) a++
#define decr(a) a--
#define true (1)
#define false (0)
#define round(a) ((int)(a+.5))
#define ptc(a,b) putc((char)(a),b)
typedef int integer ;
typedef short boolean ;
typedef FILE *bytefile ;
bytefile gffile ;
bytefile pxlfile ;
integer pxlloc ;
char gfname[81], pxlname[81] ;
integer mem[4001] ;
integer power[17] ;
integer gfch ;
integer checksum ;
integer dirptr ;
integer designsize ;
integer pxlmag ;
int lastext = 0 ;
initialize () { integer i ;
printf ( "This is GFread, C Version 0.0\n" ) ;
for ( i = 0 ; i <= 639 ; i ++ ) mem [ i ] = 0 ;
power [ 0 ] = 1 ;
for ( i = 1 ; i <= 16 ; i ++ ) power [ i ] = power [ i - 1 ] * 2 ;
}
jumpout () { exit ( 1 ) ;
}
opengffile () { gffile = fopen ( gfname , "r" ) ;
}
openpxlfile () { pxlfile = fopen ( pxlname , "w" ) ;
pxlloc = 0 ;
}
integer gfbyte () { integer i ;
i = getc ( gffile ) ;
if ( i < 0 ) return ( - i ) ;
else return ( i ) ;
}
integer gfsignedquad () { integer i ;
i = gfbyte () ;
if ( i > 127 ) i = i - 256 ;
i = 256 * i + gfbyte () ;
i = 256 * i + gfbyte () ;
return ( 256 * i + gfbyte () ) ;
}
pxlhalfword ( w )
integer w ;
{ if ( w < 0 ) w = w + 65536 ;
{ ptc ( w / 256 , pxlfile ) ;
incr ( pxlloc ) ;
}
{ ptc ( w % 256 , pxlfile ) ;
incr ( pxlloc ) ;
}
}
pxlword ( w )
integer w ;
{ if ( w > 0 )
{ ptc ( w / 16777216 , pxlfile ) ;
incr ( pxlloc ) ;
}
else
{ w = w + 1073741824 ;
w = w + 1073741824 ;
{ ptc ( ( w / 16777216 ) + 128 , pxlfile ) ;
incr ( pxlloc ) ;
}
}
{ ptc ( ( w / 65536 ) % 256 , pxlfile ) ;
incr ( pxlloc ) ;
}
{ ptc ( ( w / 256 ) % 256 , pxlfile ) ;
incr ( pxlloc ) ;
}
{ ptc ( w % 256 , pxlfile ) ;
incr ( pxlloc ) ;
}
}
loadgffile () { integer gfchar ;
integer i, j, k ;
integer gfcom ;
integer minm, maxm ;
integer minn, maxn ;
integer rows ;
boolean on ;
boolean state ;
integer extra ;
boolean bad ;
integer maxrows ;
integer wordwidth ;
integer word ;
integer count ;
integer bit ;
integer hppp, vppp ;
integer dx, dy ;
opengffile () ;
if ( gfbyte () != 247 )
{ printf ( " Bad GF file: First byte is not preamble!\n" ) ;
jumpout () ;
}
if ( gfbyte () != 131 )
{ printf ( " Bad GF file: Identification byte is incorrect!\n" ) ;
jumpout () ;
}
i = gfbyte () ;
for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ;
do { gfcom = gfbyte () ;
switch ( gfcom )
{ case 67 :
case 68 :
{ if ( gfcom == 67 )
{ gfch = gfsignedquad () ;
i = gfsignedquad () ;
minm = gfsignedquad () ;
maxm = gfsignedquad () ;
minn = gfsignedquad () ;
maxn = gfsignedquad () ;
}
else
{ gfch = gfbyte () ;
i = gfbyte () ;
maxm = gfbyte () ;
minm = maxm - i ;
i = gfbyte () ;
maxn = gfbyte () ;
minn = maxn - i ;
}
if ( gfch > 127 ) do { gfcom = gfbyte () ;
switch ( gfcom )
{ case 239 :
case 240 :
case 241 :
case 242 :
{ i = 0 ;
for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + gfbyte
() ;
for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ;
}
break ;
case 243 :
k = gfsignedquad () ;
break ;
case 66 :
case 73 :
i = gfbyte () ;
case 65 :
case 72 :
i = gfbyte () ;
case 64 :
case 71 :
i = gfbyte () ;
break ;
default : ;
} } while ( ! ( gfcom == 69 ) ) ;
else
{ maxrows = memmax - 640 ;
bad = false ;
rows = 0 ;
on = false ;
extra = 0 ;
state = true ;
do { gfcom = gfbyte () ;
if ( gfcom >= 1 && gfcom <= 66 )
{ if ( gfcom < 64 ) i = gfcom - 0 ;
else
{ i = 0 ;
for ( j = 0 ; j <= gfcom - 64 ; j ++ ) i = i * 256 +
gfbyte () ;
}
if ( state )
{ extra = extra + i ;
state = false ;
}
else
{
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = extra ;
incr ( rows ) ;
}
}
extra = i ;
}
on = ! on ;
}
else if ( gfcom >= 74 && gfcom <= 238 )
{ if ( ! on && ( extra > 0 ) )
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = extra ;
incr ( rows ) ;
}
}
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = 65535 ;
incr ( rows ) ;
}
}
on = true ;
extra = gfcom - 74 ;
state = false ;
}
else
switch ( gfcom )
{ case 0 :
{ state = ! state ;
on = ! on ;
}
break ;
case 70 :
case 71 :
case 72 :
case 73 :
{ i = 0 ;
for ( j = 1 ; j <= gfcom - 70 ; j ++ ) i = i * 256 + gfbyte
() ;
if ( ! on && ( extra > 0 ) )
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = extra ;
incr ( rows ) ;
}
}
for ( j = 0 ; j <= i ; j ++ )
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = 65535 ;
incr ( rows ) ;
}
}
on = false ;
extra = 0 ;
state = true ;
}
case 239 :
case 240 :
case 241 :
case 242 :
{ i = 0 ;
for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 +
gfbyte () ;
for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ;
}
break ;
case 243 :
k = gfsignedquad () ;
break ;
case 247 :
;
break ;
case 69 :
{ if ( bad )
{ printf (
" Ran out of internal memory for row counts!" ) ;
jumpout () ;
}
if ( ! on && ( extra > 0 ) )
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = extra ;
incr ( rows ) ;
}
}
if ( ( rows > 0 ) && ( mem [ rows + 639 ] != 65535 ) )
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = 65535 ;
incr ( rows ) ;
}
}
{ if ( rows > maxrows ) bad = true ;
else
{ mem [ rows + 640 ] = 65534 ;
incr ( rows ) ;
}
}
i = 0 ;
decr ( rows ) ;
mem [ gfch + 512 ] = pxlloc / 4 ;
while ( mem [ i + 640 ] == 65535 ) incr ( i ) ;
if ( mem [ i + 640 ] != 65534 )
{ maxn = maxn - i ;
while ( mem [ rows + 638 ] == 65535 )
{ decr ( rows ) ;
mem [ rows + 640 ] = 65534 ;
}
minn = maxn + 1 ;
extra = maxm - minm + 1 ;
maxm = 0 ;
j = i ;
while ( mem [ j + 640 ] != 65534 )
{ decr ( minn ) ;
if ( mem [ j + 640 ] != 65535 )
{ k = mem [ j + 640 ] ;
if ( k < extra ) extra = k ;
incr ( j ) ;
while ( mem [ j + 640 ] != 65535 )
{ k = k + mem [ j + 640 ] ;
incr ( j ) ;
}
if ( maxm < k ) maxm = k ;
}
incr ( j ) ;
}
minm = minm + extra ;
maxm = minm + maxm - 1 ;
mem [ gfch + 128 ] = maxn - minn + 1 ;
mem [ gfch ] = maxm - minm + 1 ;
mem [ gfch + 256 ] = - minm ;
mem [ gfch + 384 ] = maxn ;
wordwidth = ( maxm - minm + 32 ) / 32 * 2 ;
while ( mem [ i + 640 ] != 65534 )
{ j = 0 ;
word = 0 ;
bit = 16 ;
on = false ;
count = mem [ i + 640 ] - extra ;
while ( count != 65535 )
{ incr ( i ) ;
while ( count > 0 )
{ if ( count >= bit )
{ if ( on ) word = word + power [ bit ] - 1 ;
count = count - bit ;
pxlhalfword ( word ) ;
incr ( j ) ;
word = 0 ;
bit = 16 ;
}
else
{ if ( on ) word = word + power [ bit ] -
power [ bit - count ] ;
bit = bit - count ;
count = 0 ;
}
}
on = ! on ;
count = mem [ i + 640 ] ;
}
while ( j < wordwidth )
{ pxlhalfword ( word ) ;
word = 0 ;
incr ( j ) ;
}
incr ( i ) ;
}
}
}
break ;
} } while ( ! ( gfcom == 69 ) ) ;
}
}
break ;
case 239 :
case 240 :
case 241 :
case 242 :
{ i = 0 ;
for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + gfbyte () ;
for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ;
}
break ;
case 243 :
k = gfsignedquad () ;
break ;
case 247 :
;
break ;
case 248 :
;
break ;
default :
{ printf (
" Bad GF file: Unexpected %d command between characters!\n" , gfcom ) ;
jumpout () ;
}
break ;
} } while ( ! ( gfcom == 248 ) ) ;
for ( gfch = 0 ; gfch <= 127 ; gfch ++ ) mem [ gfch + 640 ] = 0 ;
i = gfsignedquad () ;
designsize = gfsignedquad () ;
checksum = gfsignedquad () ;
hppp = gfsignedquad () ;
vppp = gfsignedquad () ;
if ( hppp != vppp ) printf ( "Odd aspect ratio!\n" ) ;
pxlmag = round ( ( ( 5 * hppp ) * 72.27 ) / 65536.0 ) ;
i = gfsignedquad () ;
i = gfsignedquad () ;
i = gfsignedquad () ;
i = gfsignedquad () ;
do { gfcom = gfbyte () ;
switch ( gfcom )
{ case 245 :
case 246 :
{ gfch = gfbyte () ;
if ( gfcom == 245 )
{ dx = gfsignedquad () ;
dy = gfsignedquad () ;
}
else
{ dx = gfbyte () * 65536 ;
dy = 0 ;
}
mem [ gfch + 640 ] = gfsignedquad () ;
i = gfsignedquad () ;
}
break ;
case 239 :
case 240 :
case 241 :
case 242 :
{ i = 0 ;
for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + gfbyte () ;
for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ;
}
break ;
case 243 :
k = gfsignedquad () ;
break ;
case 247 :
;
break ;
case 249 :
;
break ;
default :
{ printf ( " Bad GF file: Unexpected %d in postamble!\n" , gfcom ) ;
jumpout () ;
}
break ;
} } while ( ! ( gfcom == 249 ) ) ;
}
main (argc, argv)
int argc;
char *argv[] ;
{ if ( argc < 2 || argc > 3 )
{ printf("Usage: gfread gffile[.gf] [pxlfile[.nnnnpxl]]\n") ;
jumpout () ; }
{ int i ;
strcpy(gfname, argv[1]);
for (i=0;gfname[i]!=0;i++)
if ( gfname[i] == '/' ) lastext = 0 ;
else if ( gfname[i] == '.' && lastext == 0 ) lastext = i ;
strcpy(pxlname, gfname) ;
if ( lastext == 0 ) {
strcpy(gfname+i, ".gf") ;
lastext =i ; }
strcpy(pxlname+lastext, ".pxl") ;
if (argc == 3 ) strcpy(pxlname, argv[2]) ; }
initialize () ;
openpxlfile () ;
pxlword ( 1001 ) ;
loadgffile () ;
dirptr = pxlloc / 4 ;
for ( gfch = 0 ; gfch <= 127 ; gfch ++ )
{ pxlhalfword ( mem [ gfch ] ) ;
pxlhalfword ( mem [ gfch + 128 ] ) ;
pxlhalfword ( mem [ gfch + 256 ] ) ;
pxlhalfword ( mem [ gfch + 384 ] ) ;
pxlword ( mem [ gfch + 512 ] ) ;
pxlword ( mem [ gfch + 640 ] ) ;
}
pxlword ( checksum ) ;
pxlword ( pxlmag ) ;
pxlword ( designsize ) ;
pxlword ( dirptr ) ;
pxlword ( 1001 ) ;
fclose(pxlfile) ;
if ( argc == 2 ) {
strcpy(gfname, pxlname) ;
sprintf(gfname+lastext, ".%dpxl", pxlmag) ;
rename(pxlname, gfname) ; }
lab9999 : ;
}